package com.wish.g04.renting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g04.base.dto.SfContractDTO;
import com.wish.g04.base.entity.SfContract;
import com.wish.g04.base.entity.User;
import com.wish.g04.base.service.ISfContractService;
import com.wish.g04.base.service.IUserService;
import com.wish.g04.base.vo.SfContractVo;
import com.wish.g04.common.core.web.controller.BaseController;
import com.wish.g04.common.core.web.domain.AjaxResult;
import com.wish.g04.common.core.web.page.TableDataInfo;
import com.wish.g04.common.security.annotation.PreAuthorize;
import com.wish.g04.renting.service.PaymentPlanService;
import com.wish.g04.renting.service.TSfContractService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 这个类的作者是123
 * 开发的日期是：2022/05/27
 **/
@Service
public class TSfContractServiceImpl extends BaseController implements TSfContractService {
    @Autowired
    ISfContractService iSfContractService;
    @Autowired
    IUserService iUserService;


    @Override
    public TableDataInfo getlist(SfContractDTO sfContractDTO) {
        QueryWrapper<SfContract> wrapper = new QueryWrapper();
        //收房合同编号
        if(StringUtils.hasText(sfContractDTO.getSfContractNo())){
            wrapper.like("sf_contract_no",sfContractDTO.getSfContractNo());
        }
        //小区名称
        if(StringUtils.hasText(sfContractDTO.getPremiseName())){
            wrapper.like("premise_name",sfContractDTO.getPremiseName());
        }
        //业主姓名
        if(StringUtils.hasText(sfContractDTO.getOwnerName())){
            wrapper.like("owner_name",sfContractDTO.getOwnerName());
        }
        //合同录入日期--至--
        if(sfContractDTO.getLeaseStartTime()!=null){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(sfContractDTO.getLeaseStartTime());
            wrapper.ge("lease_start_time",format);
        }
        if(sfContractDTO.getLeaseEndTime()!=null){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(sfContractDTO.getLeaseEndTime());
            wrapper.le("lease_end_time",format);
        }
        List<SfContract> list = iSfContractService.list(wrapper);
        int count = iSfContractService.count();
        List<SfContractVo> sfContractVos = setUserName(list);
        TableDataInfo dataTable = getDataTable(sfContractVos);
        dataTable.setTotal(Long.parseLong(count+""));
        return dataTable;
    }

    @Override
    public List<SfContractVo> setUserName(List<SfContract> list) {
        //转vo对象
        ArrayList<SfContractVo> arrayList = new ArrayList<>();
        list.forEach(sfContract -> {
            SfContractVo sfContractVo = new SfContractVo();
            BeanUtils.copyProperties(sfContract,sfContractVo);
            arrayList.add(sfContractVo);
        });
        //获取userid集合并查询
        List<Long> userIdList = arrayList.stream().map(sfContractVo -> sfContractVo.getCreateUserid()).collect(Collectors.toList());
        if(userIdList.size()>0){
            List<User> users = iUserService.listByIds(userIdList);
            //设置创建人
            for (int i = 0; i <users.size() ; i++) {
                User user = users.get(i);
                arrayList.get(i).setUsername(user.getUsername());
            }
        }

        return arrayList;
    }

    @Override
    public int insertTSfContract(SfContract sfContract) {

        return 0;
    }

    @Override
    public boolean updateXyStatus(Long id) {
        SfContract sfContract = new SfContract();
        sfContract.setId(id);
        sfContract.setXyStatus(true);
        boolean b = iSfContractService.updateById(sfContract);
        return b;
    }


}
